Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
Not bad, the methods you wrote are good, but your time and space are a little off. There are some better ways to do things more efficiently. Take a look at my answer for reverse_in_place for a hint.
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the size of the number input | ||
| # Space complexity: O(n) where n is the size of the number input | ||
| def factorial(n) |
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the string |
There was a problem hiding this comment.
This is actually O(n^2) for time and space due to the fact that s[1..-1] creates a new string with each recursive call.
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the number of bunnies | ||
| # Space complexity: O(n) where n is the number of bunnies | ||
| def bunny(n) |
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| def reverse_inplace(s) | ||
| raise NotImplementedError, "Method not implemented" |
There was a problem hiding this comment.
Think about making a helper which takes the string, a left index and right index. The base case is when left_index >= right_index Each recursive call you swap the characters at left_index or right_index and then make a recursive call with the string, and left_index + 1 and right_index -1.
| return false | ||
| elsif s.length == 0 | ||
| return true | ||
| # chris!! I realize this would fail if given an initial string like ")(" but i can't figure it out! |
There was a problem hiding this comment.
Take a look at my notes for the reverse in place method and a similar solution can work here.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the length of the array |
There was a problem hiding this comment.
This is O(n^2) due to the fact that you create a new array with each recursive call.
|
|
||
| # Time complexity: ? | ||
| # Space complexity: ? | ||
| # Time complexity: O(n) where n is the size of the number |
No description provided.